package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.PeachblossomSound;
import com.itheima.domain.vo.SoundVo;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.SoundService;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@Component
public class SoundManager {

    @Autowired
    private FastFileStorageClient client;  //存储的客户端  注入会报错 , 不影响执行
    @Autowired
    private FdfsWebServer webServer; // tracker的服务器  注入会报错 , 不影响执行

    @Reference
    private SoundService soundService;
    @Reference
    private UserInfoService userInfoService;
    
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 发送语音
     */
    public Map sendSound(MultipartFile sound) throws IOException {
        //1.上传语音到fastDFS
        System.out.println(sound.getInputStream());
        System.out.println(sound.getSize());
        StorePath storePath = client.uploadFile(sound.getInputStream(), sound.getSize(), FileUtil.extName(sound.getOriginalFilename()), null);
        String soundUrl = webServer.getWebServerUrl() + storePath.getFullPath();//拼接文件路径
        System.err.println("------->>>"+soundUrl);
        //2.构建语音对象   created在实现类中在赋值
        PeachblossomSound peachSound = new PeachblossomSound();
        peachSound.setSoundUrl(soundUrl);
        peachSound.setUserId(UserHolder.getId());
        //3.调用service保存语音到语音表
        soundService.saveSound(peachSound);
        return null;
    }
    

    /**
     * 接收语音
     * @return
     */
    public SoundVo inceptSound() {
        SoundVo soundVo = new SoundVo();
        String key = ConstantUtil.Sound_USER + UserHolder.getId();
        //1.判断当前用户今天是否接收过语音
        Boolean hasKey = stringRedisTemplate.hasKey(key);
        Integer remainingTimes;//声明剩余次数
        //1.1今天没有接收语音
        if (!hasKey) {
             remainingTimes=9;
            //将key存入Redis中并设置过期时间
            //todo 过期时间挺麻烦,定时任务零点直接删除键
            stringRedisTemplate.opsForValue().set(key,remainingTimes+"");
            //调用service获取语音
            PeachblossomSound peachSound=soundService.findSound(UserHolder.getId());
            if (peachSound==null) {
                return soundVo;
            }
            //封装vo
            UserInfo userInfo = userInfoService.findById(peachSound.getUserId());
            BeanUtil.copyProperties(userInfo,soundVo);
            soundVo.setSoundUrl(peachSound.getSoundUrl());
            soundVo.setRemainingTimes(remainingTimes);

        } else{
            remainingTimes = Integer.valueOf(stringRedisTemplate.opsForValue().get(key));
            if (remainingTimes>0) {
                //刷新剩余次数
                remainingTimes=remainingTimes-1;
                stringRedisTemplate.opsForValue().set(key,remainingTimes+"");
                //调用service获取语音
                PeachblossomSound peachSound=soundService.findSound(UserHolder.getId());
                if (peachSound==null) {
                    return soundVo;
                }
                //封装vo
                UserInfo userInfo = userInfoService.findById(peachSound.getUserId());
                BeanUtil.copyProperties(userInfo,soundVo);
                soundVo.setSoundUrl(peachSound.getSoundUrl());
                soundVo.setRemainingTimes(remainingTimes);
            }
        }
        return soundVo;
    }
}
